Use Windows.pkg
Use DFClient.pkg
Use cDbCJGrid.pkg
Use dfEntry.pkg
Use cAlbumDataDictionary.dd
Use cSongDataDictionary.dd
Use dfallent.pkg
Use DeleteAll.bp
Use dfTable.pkg

Deferred_View Activate_oSongs for ;
Object oSongs is a dbView
    
    Property String psMboxCaption "Song View"
    Set Border_Style to Border_Thick
    Set Size to 295 622
    Set Location to 2 2
    Set Label to "Song View"
    Set Icon to "music.ico"
    
    Object oAlbum_DD is a cAlbumDataDictionary
    End_Object

    Object oSong_DD is a cSongDataDictionary
        Set Constrain_file to Album.File_number
        Set DDO_Server to oAlbum_DD
        
        Function Validate_Delete Returns Integer
            Integer iError
            Forward Get Validate_Delete to iError
            If (not(iError)) Begin
                If (Album.TOTAL_TRACKS=1) Begin //attempting to delete last track in an album
                    Move 1 to iError
                    Error DFERR_OPERATOR "Cannot delete last song in album. Please delete album instead."
                End
            End
            Function_Return iError
        End_Function
    End_Object

    Set Main_DD to oAlbum_DD
    Set Server to oAlbum_DD

    Object oAlbumGroup is a dbGroup
        Set Size to 54 461
        Set Location to 6 8
        Set Label to "Album:"

        Object oAlbum_TITLE is a dbForm
            Entry_Item Album.TITLE
            Set Location to 10 43
            Set Size to 13 403
            Set Label to "Title:"
            Set Label_Col_Offset to 0
            Set Label_Justification_Mode to JMode_Right
        End_Object
        Object oAlbum_TOTAL_TRACKS is a dbForm
            Entry_Item Album.TOTAL_TRACKS
            Set Location to 34 68
            Set Size to 13 45
            Set Label to "Total Tracks:"
            Set Label_Col_Offset to 0
            Set Label_Justification_Mode to JMode_Right
        End_Object
        Object oAlbum_TOTAL_TIME is a dbForm
            Entry_Item Album.TOTAL_TIME
            Set Location to 34 215
            Set Size to 13 55
            Set Label to "Total Time:"
            Set Label_Justification_Mode to JMode_Right
            Set Label_Col_Offset to 0
        End_Object
        Object oAlbum_RATING is a dbComboForm
            Entry_Item Album.RATING
            Set Location to 34 404
            Set Size to 13 42
            Set Label to "Rating:"
            Set Label_Col_Offset to 0
            Set Label_Justification_Mode to JMode_Right
        End_Object
    End_Object

    Object oSongGrid is a dbGrid
        Set Server to oSong_DD
        Set Size to 187 608
        Set Location to 69 8
        Set peAnchors to anAll
        Set Child_Table_State to True
        
        Begin_Row
            Entry_Item Song.TRACK_NUM
            Entry_Item Song.NAME
            Entry_Item Song.ARTIST
            Entry_Item Song.COMPOSER
            Entry_Item Song.GENRE
            Entry_Item Song.SIZE
            Entry_Item Song.Time
            Entry_Item Song.YEAR
            Entry_Item Song.KIND
        End_Row

        Set Main_File to Song.File_Number

        Set Form_Width 0 to 29
        Set Header_Label 0 to "Track"
        Set Form_Width 1 to 143
        Set Header_Label 1 to "Name"
        Set Form_Width 2 to 92
        Set Header_Label 2 to "Artist"
        Set Form_Width 3 to 123
        Set Header_Label 3 to "Composer"
        Set Form_Width 4 to 52
        Set Header_Label 4 to "Genre"
        Set Form_Width 5 to 46
        Set Header_Label 5 to "Size"
        Set Form_Width 6 to 27
        Set Header_Label 6 to "Time"
        Set Form_Width 7 to 29
        Set Header_Label 7 to "Year"
        Set Form_Width 8 to 59
        Set Header_Label 8 to "Kind"
        Set peResizeColumn to rcAll
        Set CurrentRowColor to clHighlight
        Set CurrentRowTextColor to clHighlightText

        Function Child_entering Returns Integer
            Integer iRetval
            
            Delegate Get SaveHeader to iRetval
            Function_Return iRetval
        End_Function
        
        Function Confirm_Delete_Song Returns Integer
            Integer iRetval
            String sMessage
            
            Move "Do you wish to delete this song?" to sMessage
            Get Confirm sMessage to iRetval
            Function_Return iRetval
        End_Function
        
        Set Verify_Delete_msg to get_Confirm_Delete_Song
    End_Object

    Object oSortBy is a RadioGroup
        Set Location to 265 48
        Set Size to 27 105
        Set Label to "Sort By:"
        Set peAnchors to anBottomLeft
    
        Object oRadio1 is a Radio
            Set Label to "Track Number"
            Set Size to 10 61
            Set Location to 11 5
        End_Object
    
        Object oRadio2 is a Radio
            Set Label to "Name"
            Set Size to 10 61
            Set Location to 11 65
        End_Object
    
        Procedure Notify_Select_State Integer iToItem Integer iFromItem
            Forward Send Notify_Select_State iToItem iFromItem
            If (iToItem=0) Set Ordering of oSongGrid to Index.1
            If (iToItem=1) Set Ordering of oSongGrid to Index.2
            Send Beginning_of_Data to oSongGrid
        End_Procedure
    
    End_Object

    Object oDeleteGroup is a Group
        Set Size to 30 91
        Set Location to 262 475
        Set Label to "Delete All Music Data"
        Set peAnchors to anBottomRight

        Object oDeleteAllBtn is a Button
            Set Location to 12 16
            Set Label to "&Delete All"
            Set peAnchors to anBottomRight
        
            Procedure OnClick
                Integer iChoice
                Get Message_Box "Are you sure you wish to delete all music data? (This operation cannot be undone.)" (psMboxCaption(Self)) MB_OKCANCEL MB_ICONASTERISK to iChoice
                If (iChoice=MBR_Ok) Begin
                    Send DoDeleteAll of oDeleteAll
                    Send Beginning_of_Data to oSongGrid
                    Send Clear of oAlbum_DD
                End
            End_Procedure
        End_Object
    End_Object
    
    Function Confirm_Save_Album Returns Integer
        Integer iRetVal iSrvr iRec
        String sMsg
        
        Get Server to iSrvr
        Get Current_Record of iSrvr to iRec
        If (iRec=0) Begin
            Move "Save new album?" to sMsg
            Get Confirm sMsg to iRetVal
            Function_Return iRetVal            
        End
    End_Function
    
    Function Confirm_Delete_Album Returns Integer
        Integer iRetVal
        String sMsg
        
        Move "Are you sure you wish to delete the entire album?" to sMsg
        Get Confirm sMsg to iRetVal
        Function_Return iRetVal
    End_Function
    
    Set Verify_Save_msg to get_Confirm_Save_Album
    Set Verify_Delete_msg to get_Confirm_Delete_Album
    
    Function SaveHeader Returns Integer
        Integer iRec bChanged iSrvr
        String sError_String
        
        Get Server to iSrvr
        Get Current_Record of iSrvr to iRec
        Get Should_Save to bChanged
        
        If (iRec=0 and bChanged=0) Begin
            Move "First create and save an album, or select an album from the Title dialog." to sError_String
            Error DFERR_OPERATOR sError_String
            
            Function_Return 1
        End
        Send Request_Save_No_Clear
        
        Get Should_Save to bChanged
        If (iRec=0 or bChanged) Function_Return 1
        Else Function_Return 0
    End_Function
    
    On_Key Key_Alt+Key_D Send OnClick of oDeleteAllBtn

Cd_End_Object
